package org.litemall.netty.nettyDoman;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * @Author Lizr
 * @Description 消息内容
 * @CreateTime 2021-12-17 下午 3:14
 **/
@Data
public class SocketMessage<T> {
    private int code;
    private String msg;
    /**
     * 消息类型 ， 可选值:
     *  1 连接成功
     *  2 用户通知消息
     *  3 心跳消息
     *  4 连接关闭
     */
    private int type;
    private T data;

    private SocketMessage(int code, String msg, int type, T data){
        this.code = code;
        this.msg = msg;
        this.type = type;
        this.data = data;
    }

    /**
     * 发送连接成功消息
     * @return
     */
    public static SocketMessage sendJoinMessage(){
        return new SocketMessage(0, "连接成功", 1, null);
    }


    /**
     * 发送监听消息
     * @return
     */
    public static SocketMessage sendBeatMessage(){
        return new SocketMessage(0, "beat-" + System.currentTimeMillis(), 3, null);
    }

    /**
     * 发送连接关闭消息
     * @return
     */
    public static SocketMessage sendClonseMessage(){
        return new SocketMessage(0, "读写空闲次数过多，强制关闭", 4, null);
    }
    /**
     * 发送用户通知消息
     * @return
     */
    public static SocketMessage sendUserNotice(Object data){
        return new SocketMessage(0, "成功", 2, data);
    }

    /**
     * 重写tostring方法，返回json字符串
     * @return
     */
    @Override
    public String toString(){
        return JSONObject.toJSONString(this);
    }
}
